package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallFootprint;
import com.cskaoyan.bean.CskaoyanmallFootprintExample;
import com.cskaoyan.bean.CskaoyanmallGoods;
import com.cskaoyan.bean.vo.FootprintListVo;
import com.cskaoyan.bean.vo.WxFootprintVo;
import com.cskaoyan.mapper.CskaoyanmallFootprintMapper;
import com.cskaoyan.mapper.CskaoyanmallGoodsMapper;
import com.cskaoyan.service.WxFootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class WxFootprintServiceImpl implements WxFootprintService {

    @Autowired
    CskaoyanmallFootprintMapper footprintMapper;
    
    @Autowired
    CskaoyanmallGoodsMapper goodsMapper;

    @Override
    public WxFootprintVo queryFoot(Integer page, Integer size, Integer userId) {

        PageHelper.startPage(page, size);

        CskaoyanmallFootprintExample cskaoyanmallFootprintExample = new CskaoyanmallFootprintExample();
        CskaoyanmallFootprintExample.Criteria criteria = cskaoyanmallFootprintExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);

        cskaoyanmallFootprintExample.setOrderByClause("id" + " " + "desc");
        List<CskaoyanmallFootprint> footprints = footprintMapper.selectByExample(cskaoyanmallFootprintExample);
        PageInfo<CskaoyanmallFootprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();

        ArrayList<FootprintListVo> footprintListVos = new ArrayList<>();

        Iterator<CskaoyanmallFootprint> iterator = footprints.iterator();

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int flag = 0;
        while (iterator.hasNext()) {
            CskaoyanmallFootprint next = iterator.next();
            FootprintListVo temp = new FootprintListVo();

            temp.setAddTime(sf.format(next.getAddTime()));
            temp.setGoodsId(next.getGoodsId());
            temp.setId(next.getId());

            footprintListVos.add(temp);
        }

//        根据footprintListVos每一项的goodsId查询出brief、picUrl、name、retailPrice
        for (int i = 0; i < footprintListVos.size(); i++) {

            FootprintListVo temp = footprintListVos.get(i);
            CskaoyanmallGoods goods = goodsMapper.selectByPrimaryKey(temp.getGoodsId());
            temp.setBrief(goods.getBrief());
            temp.setName(goods.getName());
            temp.setPicUrl(goods.getPicUrl());
            temp.setRetailPrice(goods.getRetailPrice().intValue());
        }

        WxFootprintVo footprintVo = new WxFootprintVo();
        footprintVo.setTotalPages(total);
        footprintVo.setFootprintList(footprintListVos);



        return footprintVo;
    }

    @Override
    public void deleteFoot(Integer id, Integer userId) {

//        TODO: 做逻辑删除
        footprintMapper.deleteByPrimaryKey(id);

    }
}
